﻿var titBook = titBook || {};
titBook.module = titBook.module || {};

titBook.core.register("rightContent-module", function (sandbox) {
    return {
        activate: function () {
            var itemsVM = new titBook.viewModels.rightContentViewModel(sandbox, this._dataUpdate);
            sandbox.bind(itemsVM);



            //titBook.core.communications.addListener("someEvent", function () { alert("event was handled"); }, "someContext");
            //titBook.core.communications.notify("someEvent");
        },
        destroy: function () {

        },

        _dataUpdate: function (answer) {
            var parent = $('#' + sandbox.getDomElement());

            var container = parent;
            container[0].innerHTML = "";

            for (var i = 0; i < answer.length; i++) {
                var id = "rightContentList" + i,
                    data = answer[i];
                var box = document.createElement("div");
                box.setAttribute("class", "rightContentList");
                box.setAttribute("id", id);
                container.append(box);
                //container.html("<div class='rightContentList' id=" + id + " ><div>");
                var child = $("#" + id);
                var dataSource = new kendo.data.DataSource({
                    data: data
                });

                child.kendoListView({
                    dataSource: dataSource,
                    template: '<div class="item"><div class="item-left"><span class="rightContentListId">#:Name#: </span></div><div class="item-right"><span class="rightContentListValue">#:Value#</span></div></div><br/>',
                    autobind: false
                });
                var spanElem = document.createElement("span");
                spanElem.setAttribute("id", "span-Name");
                var aElem = document.createElement("a");
                var imgElem = document.createElement("img");
                imgElem.setAttribute("src", "../Content/img/pencil.png");
                aElem.appendChild(imgElem);
                aElem.setAttribute("href", "#");
                spanElem.appendChild(document.createTextNode("Item " + parseInt(i + 1)));
                var parentDiv = document.getElementById(id);
                var newDiv = document.createElement("div");
                newDiv.setAttribute("id", "sc-right-newDiv");
                parentDiv.insertBefore(newDiv, parentDiv.childNodes[0]);

                //parentDiv.appendChild(document.createElement("div"));
                newDiv.appendChild(aElem);
                newDiv.appendChild(spanElem);
            }
        }
        
        //_addItemBox: function(data, container) {
        //    container.html("<div id='rightContentList'><div>");
        //    var child = $("#rightContentList");
        //    var dataSource = new kendo.data.DataSource({
        //        data: data
        //    });
        //    child.kendoListView({
        //        dataSource: dataSource,
        //        template: '<div class="item"><div class="item-left"><span class="rightContentListId">#:Name#: </span></div><div class="item-right"><span class="rightContentListValue">#:Value#</span></div></div><br/>',
        //        autobind: false
        //    });
        //    var spanElem = document.createElement("span");
        //    spanElem.setAttribute("id", "span-Name");
        //    var aElem = document.createElement("a");
        //    var imgElem = document.createElement("img");
        //    imgElem.setAttribute("src", "../Content/img/pencil.png");
        //    aElem.appendChild(imgElem);
        //    aElem.setAttribute("href", "#");
        //    spanElem.appendChild(document.createTextNode("Name"));
        //    var parentDiv = document.getElementById("rightContentList");
        //    var newDiv = document.createElement("div");
        //    newDiv.setAttribute("id", "sc-right-newDiv");
        //    parentDiv.insertBefore(newDiv, parentDiv.childNodes[0]);

        //    //parentDiv.appendChild(document.createElement("div"));
        //    newDiv.appendChild(aElem);
        //    newDiv.appendChild(spanElem);
        //}
    };
});